package StringType;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author ChanZany
 * @Date 2021/5/11 12:19
 * @Version 1.0
 * <p>
 * 1.三个同样的字母连在一起，一定是拼写错误，去掉一个的就好啦：比如 helllo -> hello2.两对一样的字母（AABB型）连在一起，一定是拼写错误，去掉第二对的一个字母就好啦：比如 helloo -> hello
 * 3.上面的规则优先“从左到右”匹配，即如果是AABBCC，虽然AABB和BBCC都是错误拼写，应该优先考虑修复AABB，结果为AABCC
 * 输入例子
 * 1:helloo
 * 2:wooooooow
 * 输出例子1:
 * hello
 * woow
 */
public class StringMending {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        String[] results = new String[N];
        for (int i = 0; i < N; i++) {
            results[i] = autoMend(in.next());
        }
        for (String result : results) {
            System.out.println(result);
        }
    }

    //方法1：快慢指针
    public static String autoMend(String line) {
        char[] chars = line.toCharArray();
        int k = 0;
        for (int i = 0; i < chars.length; i++) {
            //前两个元素=原字符串中前两个元素
            chars[k] = chars[i];
            k++;
            //AAA型
            if (k >= 3 && chars[k - 3] == chars[k - 2] &&
                    chars[k - 2] == chars[k - 1]) {
                k--;// AAA->AA
            }
            //AABB型
            if (k >= 4 && chars[k - 4] == chars[k - 3] && chars[k - 2] == chars[k - 1]) {
                k--;// AABB->AAB
            }
        }

        return String.valueOf(chars).substring(0, k);
    }

    //    方法2 字符串拼接
    public static String autoMend2(String line) {
        char[] chars = line.toCharArray();
        ArrayList<Character> res = new ArrayList<>();
        for (char c : chars) {
            int size = res.size();
            if (size >=2){
                if (c == res.get(size-1) && c== res.get(size-2)){continue;}
            }
            if (size >=3){
                if (c == res.get(size-1) && res.get(size-2) == res.get(size-3)){continue;}
            }
            res.add(c);
        }
        StringBuilder sb = new StringBuilder();
        res.forEach(sb::append);
        return sb.toString();
    }


}
